﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication.MyPassword
{
    public partial class Login : Form
    {
        public event EventHandler<TEventArgs> eventArgs;

        public Login()
        {
            InitializeComponent();
        }

        private void rbtCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rbtLogin_Click(object sender, EventArgs e)
        {
            if (login())
            {
                if (eventArgs != null)
                {
                    eventArgs(this, new TEventArgs());
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("UserName or Password is wrong");
            }
        }

        private bool login()
        {
            //Regex rHost = new Regex("[a-zA-Z0-9_]*");
            //Match m = rHost.Match(userName);
            //Match n = rHost.Match(password);

            //if (m.Success == false)
            //{
            //    return Result.InvalidUserName;
            //}

            //if (n.Success == false)
            //{
            //    return Result.InvalidPassword;
            //}

            //DataSet ds = SQLConnect_command("dbo.GetPassword", userName, password);

            //DataTable dt = ds.Table[0];

            //if (dt.Rows.Count == 1)
            //{
            //    return Success;
            //}
            //else if (dt.Rows.Count == 0)
            //{
            //    return No_Exist;
            //}
            //else
            //{
            //    return Fail;
            //}

            return true;
        }
        /*
         * 
         * 
         //[NonSerialized]
[Serializable]
public class MyObject
{
  public MyObject()
  {
  }
  public static void AddPropertyValue(DataTable table,string propertyPath,string propertyName,object propertiesValue,string propertiesType,string category)
  {
    if(table == NULL) 
      {
        
      }
    else
      {
        int order = 1;
        if(table.Rows.Count > 0)
          {
            order =  (int)table.Select("PropertyOrder = MAX(PropertyOrder)")[0]["PropertyOrdere"] + 1;
          }
        DataRow dr =  table.NewRow();
        dr["PropertyPath"] = propertyPath;
        dr["PropertyName"] = PropertyName;
        dr["PropertyValue"] = PropertyValue;
        dr["PropertyType"] = PropertyType;
        dr["PropertyOrder"] = order;

        table.Rows.Add(dr);
        table.AcceptChanges();
      }
  }

  public virtual void GetPropertiesToSave(DataTable propertiesToFill, bool addClassProperty)
  {
    if(propertiesToFill == null) 
      {
        throw new ArgumentNullException("propertiesToFill");
      }

    if(addClassProperty)
      {
        
      }

    //    AddPropertyValue(propertiesToFill,string.Empty,
  }
  
}


//[NonSerialized]
[Serializable]
public class PasswordUI : MyObject
{
  private string userName;
  private string password;
  private string siteName;
  private string other;
  public PasswordUI()
    {
      
    }

  /// <summary>
  ///
  /// </summary>
  public string UserName
  {
    get {return userName;} 
    set {userName = value;}
  }
  /// <summary>
  ///
  /// </summary>
  public string Password
  {
    get {return password;} 
    set {password = value;}
  }
  /// <summary>
  ///
  /// </summary>
  public string SiteName
  {
    get {return siteName;} 
    set {siteName = value;}
  }
  /// <summary>
  ///
  /// </summary>
  public string Other
  {
    get {return other;} 
    set {other = value;}
  }

  public override void GetPropertiesToSave(DataTable propertiesToFill, bool addClassProperty) 
  {
    if (propertiesToFill == null)
      {
        throw new ArgumentNullException("propertiesToFill");
      }

    if (addClassProperty)
      {
        AddPropertyValue(propertiesToFill, string.Empty , "__ClassName", this.GetType().FullName);
      }

    AddPropertyValue(propertiesToFill, string.Empty  , "UserName", UserName);
    AddPropertyValue(propertiesToFill, string.Empty  , "Password", Password);
    AddPropertyValue(propertiesToFill, string.Empty  , "SiteName", SiteName);
    AddPropertyValue(propertiesToFill, string.Empty  , "Other", Other);
  }
}


//[NonSerialized]
[Serializable]
public class DataAccess
{
  public DataAccess()
  {
  }

  private void SaveObject(PasswordUI password)
  {
    DataTable dt = getObjectProperties(password);

    string xml = getTableXML(dt); 

    ExecuteNonQueryWithContext("dbo.SaveObject",xml);
  }

  private DataTable getPropertiesToSave(DataTable objectProperties)
  {
    DataTable table = new DataTable("PacomObject");
    table.Locale = CultureInfo.InvariantCulture;
    table.Columns.Add(new DataColumn("PacomObjectId", typeof(long)));
    table.Columns.Add(new DataColumn("PacomObjectPropertyId", typeof(int)));
    table.Columns.Add(new DataColumn("PacomObjectPropertyValue", typeof(string)));
    table.Columns.Add(new DataColumn("PacomObjectPropertyOrder", typeof(int)));
    table.Columns.Add(new DataColumn("PacomObjectPropertyType", typeof(string)));
    table.AcceptChanges();

    return table;
  }

  /// <summary>
  /// Creates the properties table.
  /// </summary>
  /// <returns></returns>
  private static DataTable getObjectProperties(PasswordUI newObj)
  {
    DataTable props = new DataTable();
    props.Locale = CultureInfo.InvariantCulture;
    props.Columns.Add(new DataColumn("PropertyPath", typeof(string)));
    props.Columns.Add(new DataColumn("PropertyName", typeof(string)));
    props.Columns.Add(new DataColumn("PropertyValue", typeof(object)));
    props.Columns.Add(new DataColumn("PropertyType", typeof(string)));
    props.Columns.Add(new DataColumn("PropertyOrder", typeof(int)));
    props.Columns.Add(new DataColumn("Category", typeof(string)));
    props.AcceptChanges();

    //  Get the properties to save.
    newObj.GetPropertiesToSave(props, true  );

    return (props);
  }

  private string getTableXML(DataTable sourceTable)
  {
    string ret = String.Empty;

    if (sourceTable != null && sourceTable.Rows.Count > 0)
      {
        StringBuilder sb = new StringBuilder();
        using (StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture))
          {
            sw.NewLine = String.Empty;
            sourceTable.WriteXml(sw, XmlWriteMode.IgnoreSchema);
            ret = sb.ToString();
          }
      }
    return ret;
  }
}
         */ 
    }
}
